<?php
/**
 * Created by PhpStorm.
 * User: River
 * Email: river0428@gmail.com
 * Date: 2018/10/30
 * Time: 上午10:27
 */

namespace App\Models;

use Carbon\Carbon;
use App\Models\Coupon;
use Illuminate\Database\Eloquent\Model;

class UserCoupon extends Model
{
    protected $table = 'user_coupon';

    const STATUS_NEW     = 1;
    const STATUS_USED    = 2;
    const STATUS_EXPIRED = 3;

    // 1代金券，2特权权
    const TYPE_MONEY    = 1;
    const TYPE_RECHARGE = 2;

    protected $guarded = [];

    public function recharge()
    {
        return $this->belongsTo(Recharge::class);
    }

    public function user()
    {
        return $this->belongsTo(User::class,'user_id','id');
    }

    public static function sendToUser($couponId, $userId)
    {
        return true;
    }

    public static function sendRegisterCoupon($userId)
    {
//        self::sendCouponToUser(28,$userId);
//        self::sendCouponToUser(29,$userId);
//        self::sendCouponToUser(30,$userId);
//        self::sendCouponToUser(31,$userId);
//        self::sendCouponToUser(32,$userId);

        self::sendCouponToUser(35,$userId);
        self::sendCouponToUser(36,$userId);
        self::sendCouponToUser(37,$userId);
        self::sendCouponToUser(38,$userId);
        self::sendCouponToUser(39,$userId);
        self::sendCouponToUser(40,$userId);
        self::sendCouponToUser(41,$userId);
        self::sendCouponToUser(42,$userId);
        self::sendCouponToUser(43,$userId);
        self::sendCouponToUser(44,$userId);

    }

    public static function sendParentRegisterCoupon($userId)
    {
        $regCouponIds = Configure::parentRegCoupon();
        if ($regCouponIds) {
            foreach ($regCouponIds as $couponId) {
                UserCoupon::sendCouponToUser($couponId, $userId);
            }
        }
    }

    // 发放优惠券
    public static function sendCouponToUser($couponId, $userId)
    {
        $coupon = Coupon::find($couponId);
        if (!$coupon) {			
            throw new \Exception('优惠券id:' . $couponId . '不存在');
        }		
        $userCoupon                = new UserCoupon();
        $userCoupon->coupon_id     = $couponId;
        $userCoupon->user_id       = $userId;
        $userCoupon->title         = $coupon->title;
        $userCoupon->channel       = $coupon->channel;
        $userCoupon->type          = $coupon->type;
        $userCoupon->amount        = $coupon->amount;
        $userCoupon->recharge_id   = $coupon->recharge_id;
        $userCoupon->only_recharge = $coupon->only_recharge;
        $userCoupon->min_amount    = $coupon->min_amount;
        $userCoupon->started_at    = date('Y-m-d');
        $userCoupon->months        = $coupon->months;
        $userCoupon->finished_at   = date('Y-m-d H:i:s',(strtotime(Carbon::now()->addDays($coupon->expire_days)->toDateString())+3600*24-1));
        $userCoupon->save();
        $coupon->send_count += 1;
        $coupon->save();
    }

    public function isExpired()
    {
        if ($this->status == UserCoupon::STATUS_EXPIRED) {
            return true;
        }
        $now = Carbon::now()->toDateTimeString();
        if ($this->started_at > $now OR $this->finished_at < $now) {
            if ($this->status == UserCoupon::STATUS_NEW) {
                $this->status = UserCoupon::STATUS_EXPIRED;
                $this->save();
            }
            return true;
        }
        return false;
    }

}